package com.zjk.file.controller;

import com.aliyun.oss.OSS;
import com.zjk.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: gfk
 * @create: 2022/10/18
 * @Description:
 * @FileName: OssUploadController
 * @History:
 * @自定义内容：
 */
@RestController
@RequestMapping("/file/upload")

public class OssUploadController {

      @Value("${oss.hostName}")
    private String hostName ;
      @Value("${oss.bucket}")
    private String bucket ;


      @Autowired
      private OSS OSSClient;

    @PostMapping("/oss")
    public Result nativeUpload(@RequestParam("file") MultipartFile file) {


        try {
            String filename = file.getOriginalFilename();
            OSSClient.putObject(bucket,filename,file.getInputStream());
            return new Result(true,100,hostName+filename);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.FAIL();

    }
}
